package edu.java.homework.hw10.stack;

import edu.java.homework.hw10.stack.exceptions.StackEmptyException;
import edu.java.homework.hw10.stack.exceptions.StackFullException;

public class FixedSizeStack implements Stack {

    // --- Instance variables --------------------------------------------------
    Object[] objectArray = null;
    private static final int MAX_SIZE = 100;
    private int currentSize = 0;
    private int size = 0;

    // --- Constructors --------------------------------------------------------
    /**
     * create default stack
     */
    public FixedSizeStack() {
	this(MAX_SIZE);
    }

    public FixedSizeStack(int size) {
	objectArray = new Object[size];
	this.size = objectArray.length;
	currentSize = 0;
    }

    // --- Getters and Setters -------------------------------------------------
    protected static int getMaxSize() {
	return FixedSizeStack.MAX_SIZE;
    }

    // --- Methods -------------------------------------------------------------
    @Override
    public boolean isEmpty() {
	return currentSize == 0;
    }

    @Override
    public int size() {
	return currentSize;
    }

    @Override
    public Object peek() throws StackEmptyException {
	if (isEmpty()) {
	    throw new StackEmptyException("Peek operation failed. ");
	}
	System.out.println("Peek object[" + currentSize + "]");
	return objectArray[currentSize - 1];
    }

    @Override
    public Object pop() {
	if (isEmpty()) {
	    throw new StackEmptyException("Pop operation failed. ");
	}

	System.out.println("Pop object[" + currentSize + "]");
	currentSize--;
	return objectArray[currentSize];
    }

    @Override
    public void push(Object obj) {
	if (size == currentSize) {
	    throw new StackFullException("Push operation failed. ");
	}

	objectArray[currentSize] = obj;
	currentSize++;
	System.out.println("Push object[" + currentSize + "]");
    }

}
